﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright>Copyright 2008-2011 Andreas Huber Doenni</copyright>
// Distributed under the GNU General Public License version 2 (GPLv2).
// See accompanying file License.txt or copy at http://phuse.codeplex.com/license.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Phuse.Mime
{
    using System;
    using System.Diagnostics.CodeAnalysis;
    using System.Text;

    using Phuse.Text;

    /// <summary>Provides <see cref="Encoding"/> helper methods.</summary>
    internal static class EncodingHelper
    {
        [SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase", Justification = "Pure ASCII, roundtrip normalization problems are impossible.")]
        internal static Encoding GetEncoding(string charset)
        {
            if (charset == null)
            {
                // The absence of a charset parameter must be interpreted as "us-ascii",
                // see http://www.ietf.org/rfc/rfc2046.txt, 4.1.2./1
                return EncodingObjects.ASCII;
            }

            try
            {
                return Encoding.GetEncoding(charset);
            }
            catch (ArgumentException)
            {
            }

            // us-ascii seems to be missing on WP7.
            if (charset.ToLowerInvariant().Equals(EncodingObjects.ASCII.WebName))
            {
                return EncodingObjects.ASCII;
            }

            return null;
        }
    }
}
